﻿package com.sms_vs_jok;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import com.androidquery.AQuery;
import com.baidu.mobstat.StatService;
import com.mobisage.android.agg.view.AdSageLayout;
import com.mobisage.android.agg.view.AdSageListener;
import com.mobisage.android.agg.view.AdSageManager;
import com.mobisage.android.agg.view.AdSageSize;

import edu.mit.mobile.android.imagecache.ImageCache;
import edu.mit.mobile.android.imagecache.SimpleThumbnailAdapter;

import android.common;
import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity implements AdSageListener {

	private final static String ADSAGE_LOG_TAG = "ADF";
	private String tjkey = "a03b992fd0";
	private String PublishId = "ca524e7bd0f84c34a4ef5db7006bd952";

	private LinearLayout layoutMain = null;
	private AdSageLayout adSageLayout = null;
	public AdSageSize adsize = AdSageSize.AdSageSize_FullScreen;
	public int layerout = R.layout.main;
	private AQuery aq = new AQuery(this);

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(layerout);
		layoutMain = ((LinearLayout) this.findViewById(R.id.layoutMain));
		StatService.setSessionTimeOut(120);
		adSageLayout = new AdSageLayout(this, PublishId, adsize);

		adSageLayout.setAdListener(this);
		layoutMain.addView(adSageLayout);
		AdSageLayout adgoogle = new AdSageLayout(this, PublishId,
				AdSageSize.AdSageSize_468X60);
		layoutMain.addView(adgoogle, 2);

		InitListView();

	}

	public void InitListView() {

		Intent intent = getIntent();
		String name = intent.getExtras().getString("imgname");
		String tag = intent.getExtras().getString("tag");
		Log.w("data_tag",name);
		
		if (!common.hashMap.containsKey(tag)) {
			byte[] bytes = android.util.Base64.decode(
					common.getFromAsset(this, name+".png"), Base64.DEFAULT);
			try {
				android.common.Gjson(new String(bytes));
			} catch (JSONException e) {
				Log.w("data_parseerror", e.getMessage());
				e.printStackTrace();
			}
		}

		ListView list = (ListView) findViewById(R.id.sms_listview);
		ImageCache mCache = ImageCache.getInstance(this);
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		HashMap<String, String> listdata = common.hashMap.get(tag);
		if (listdata != null) {
			Iterator<String> iterator = listdata.keySet().iterator();

			while (iterator.hasNext()) {
				try {
					String key = iterator.next();
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("id", key);
					map.put("title", listdata.get(key));
					data.add(map);
				} catch (Exception e) {
					Log.w("map_error", e.getMessage());
				}
			}
		}

		SimpleAdapter listAdapter = new SimpleAdapter(this, data,
				R.layout.item_layout, new String[] { "title" },
				new int[] { R.id.smstitle });
		list.setAdapter(listAdapter);

		list.setOnItemClickListener(new android.ListViewItemClick());

	}

	/*******************************************************************************/

	@Override
	public void onDestroy() {
		super.onDestroy();

		if (adSageLayout != null) {
			adSageLayout.destroy();
		}
	}

	/*******************************************************************************/

	@Override
	public void onResume() {
		super.onResume();
		StatService.onResume(this);
		adSageLayout.resumeAd();
		/* 发送Begin Track */
		AdSageManager.getInstance().pushSystemBeginTrack(this, PublishId);
	}

	@Override
	public void onPause() {
		super.onPause();
		/* 发送End Track */
		StatService.onPause(this);
		adSageLayout.pauseAd();
		AdSageManager.getInstance().pushSystemEndTrack(this, PublishId);
	}

	/*******************************************************************************/

	/* AdSageListener Begin */
	@Override
	public void onReceiveAd() {
		Log.d(ADSAGE_LOG_TAG, "onReceiveAd");
	}

	@Override
	public void onFailedReceiveAd() {
		Log.d(ADSAGE_LOG_TAG, "onFailedReceiveAd");
	}

	@Override
	public void onReceiveFullScreenAd() {
		Log.d(ADSAGE_LOG_TAG, "onReceiveFullScreenAd");
	}

	@Override
	public void onFailedReceiveFullScreenAd() {
		Log.d(ADSAGE_LOG_TAG, "onFailedReceiveFullScreenAd");
	}

	@Override
	public void onCloseFullAd() {
		Log.d(ADSAGE_LOG_TAG, "onCloseFullAd");
	}

	@Override
	public void onPresentScreen() {
		Log.d(ADSAGE_LOG_TAG, "onPresentScreen");
	}

	@Override
	public void onDismissScreen() {
		Log.d(ADSAGE_LOG_TAG, "onDismissScreen");
	}

	@Override
	public void onFullScreenPresentScreen() {
		Log.d(ADSAGE_LOG_TAG, "onFullScreenDismissScreen");
	}

	@Override
	public void onFullScreenDismissScreen() {
		Log.d(ADSAGE_LOG_TAG, "onFullScreenDismissScreen");
	}

	@Override
	public void onClickAd() {
		Log.d(ADSAGE_LOG_TAG, "onClickAd");
	}
	/* AdSageListener End */
}